﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InPhieu;
using System.Globalization;

namespace QLVatTu.NghiepVu
{
    public partial class frmQuanLyPhieuXuatCongTrinhDuAn : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmQuanLyPhieuXuatCongTrinhDuAn()
        {
            InitializeComponent();
        }

        private void load_dtgPhieuXuat(string dieuKien = null)
        {
            string sql = "";
            if (dieuKien != null)
            {
                sql = @"SELECT tbl_PhieuXuatKho.SoPhieu, tbl_PhieuXuatKho.NgayXuat, tbl_PhieuXuatKho.MaDoiTac, tbl_DoiTac.TenDoiTac, 
	                           tbl_PhieuXuatKho.MaKho, tbl_Kho.TenKho, tbl_PhieuXuatKho.NguoiXuat, tbl_PhieuXuatKho.DienGiai, 
	                           tbl_PhieuXuatKho.MaLoaiPhieu, tbl_LoaiPhieu.TenLoaiPhieu, tbl_PhieuXuatKho.MaCongTrinhDuAn, tbl_CongTrinhDuAn.TenCongTrinhDuAn,tbl_PhieuXuatKho.MaPhieuDeNghi, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM  tbl_DoiTac INNER JOIN 
	                          tbl_PhieuXuatKho ON tbl_DoiTac.MaDoiTac = tbl_PhieuXuatKho.MaDoiTac INNER JOIN
	                          tbl_CongTrinhDuAn ON tbl_PhieuXuatKho.MaCongTrinhDuAn = tbl_CongTrinhDuAn.MaCongTrinhDuAn INNER JOIN 
	                          tbl_Kho ON tbl_PhieuXuatKho.MaKho = tbl_Kho.MaKho INNER JOIN 
	                          tbl_LoaiPhieu ON tbl_PhieuXuatKho.MaLoaiPhieu = tbl_LoaiPhieu.MaLoaiPhieu
                        "
                        + dieuKien;
            }
            else
            {
                sql = @"SELECT tbl_PhieuXuatKho.SoPhieu, tbl_PhieuXuatKho.NgayXuat, tbl_PhieuXuatKho.MaDoiTac, tbl_DoiTac.TenDoiTac, 
	                           tbl_PhieuXuatKho.MaKho, tbl_Kho.TenKho, tbl_PhieuXuatKho.NguoiXuat, tbl_PhieuXuatKho.DienGiai, 
	                           tbl_PhieuXuatKho.MaLoaiPhieu, tbl_LoaiPhieu.TenLoaiPhieu, tbl_PhieuXuatKho.MaCongTrinhDuAn, tbl_CongTrinhDuAn.TenCongTrinhDuAn,tbl_PhieuXuatKho.MaPhieuDeNghi, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM  tbl_DoiTac INNER JOIN 
	                          tbl_PhieuXuatKho ON tbl_DoiTac.MaDoiTac = tbl_PhieuXuatKho.MaDoiTac INNER JOIN
	                          tbl_CongTrinhDuAn ON tbl_PhieuXuatKho.MaCongTrinhDuAn = tbl_CongTrinhDuAn.MaCongTrinhDuAn INNER JOIN 
	                          tbl_Kho ON tbl_PhieuXuatKho.MaKho = tbl_Kho.MaKho INNER JOIN 
	                          tbl_LoaiPhieu ON tbl_PhieuXuatKho.MaLoaiPhieu = tbl_LoaiPhieu.MaLoaiPhieu";
            }
            DataTable tbl = cn.getTable(sql);
            dtgPhieuXuat.DataSource = tbl;
        }

        private void frmQuanLyPhieuXuatCongTrinhDuAn_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
            else
            {
                MessageBox.Show("Kết nối CSDL thất bại!");
                this.Close();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmThemPhieuXuatCongTrinhDuAn frm = new frmThemPhieuXuatCongTrinhDuAn();
            frm.ShowDialog();
            load_dtgPhieuXuat();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (this.TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void dtgPhieuXuat_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgPhieuXuat.SelectedRows.Count > 0 && dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                string sql = @"SELECT row_number() over(order by tbl_ChiTietPhieuXuat.MaChiTietPhieuXuat) as STT, tbl_ChiTietPhieuXuat.MaVatTu,
                                    tbl_VatTu.TenVatTu, tbl_DonViTinh.TenDonViTinh, tbl_ChiTietPhieuXuat.SoLuong 
                                FROM tbl_ChiTietPhieuXuat INNER JOIN 
                                    tbl_VatTu ON tbl_ChiTietPhieuXuat.MaVatTu = tbl_VatTu.MaVatTu INNER JOIN 
                                    tbl_DonViTinh ON tbl_ChiTietPhieuXuat.MaDonViTinh = tbl_DonViTinh.MaDonViTinh INNER JOIN 
                                    tbl_PhieuXuatKho ON tbl_ChiTietPhieuXuat.SoPhieu = tbl_PhieuXuatKho.SoPhieu 
                                WHERE tbl_PhieuXuatKho.SoPhieu = '" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                DataTable tbl = cn.getTable(sql);
                dtgChiTiet.DataSource = tbl;

                sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                btnXacNhan.Enabled = !_XacNhan;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuat.SelectedRows.Count > 0 && dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value!=null)
            {
                string sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                if (_XacNhan == true)
                {
                    MessageBox.Show("Bạn không thể sửa phiếu đã xác nhận", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                frmThemPhieuXuatCongTrinhDuAn frm = new frmThemPhieuXuatCongTrinhDuAn();
                frm.MaPhieuXuat = dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                frm.ShowDialog();
                load_dtgPhieuXuat();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuat.SelectedRows.Count > 0)
            {
                string sql = "SELECT [XacNhan] FROM [tbl_PhieuXuatKho] WHERE [SoPhieu] = '" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                if (_XacNhan == true)
                {
                    MessageBox.Show("Bạn không thể xóa phiếu đã xác nhận.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    DialogResult msg = MessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (msg == DialogResult.Yes)
                    {
                        cn.Delete_Table("tbl_ChiTietPhieuXuat", "SoPhieu='" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                        cn.Delete_Table("tbl_PhieuXuatKho", "SoPhieu='" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                        MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        HangSo.Luu_Hanh_Dong(HangSo.TenDangNhap, HangSo.TenQuyenHan, HangSo.TenNguoiDung, "Xóa phiếu xuất công trình dự án.");
                    }
                    load_dtgPhieuXuat();
                }
            }   
        }

        private void ckbSoPhieu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbSoPhieu.Checked == true)
            {
                txtSoPhieu.Text = "";
                txtSoPhieu.Enabled = true;
            }
            else
            {
                txtSoPhieu.Text = "Tìm kiếm theo mã phiếu";
                txtSoPhieu.Enabled = false;
            }
        }

        private void ckbKhoXuat_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhoXuat.Checked == true)
            {
                cbbKhoXuat.Enabled = true;
            }
            else
            {
                cbbKhoXuat.Enabled = false;
            }
        }

        private void ckbDonViSanXuat_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbDonViSanXuat.Checked == true)
            {
                cbbDonViSanXuat.Enabled = true;
            }
            else
            {
                cbbDonViSanXuat.Enabled = false;
            }
        }

        private void ckbNguoiLap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNguoiLapPhieu.Checked == true)
            {
                txtNguoiLapPhieu.Text = "";
                txtNguoiLapPhieu.Enabled = true;
            }
            else
            {
                txtNguoiLapPhieu.Text = "Tìm theo người lập phiếu";
                txtNguoiLapPhieu.Enabled = false;
            }
        }

        private void ckbCongTrinhDuAn_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbCongTrinhDuAn.Checked == true)
            {
                cbbCongTrinhDuAn.Enabled = true;
            }
            else
            {
                cbbCongTrinhDuAn.Enabled = false;
            }
        }

        private void loadData()
        {
            cbbKhoXuat.DataSource = cn.getTable("select * from tbl_Kho");
            cbbKhoXuat.DisplayMember = "TenKho";
            cbbKhoXuat.ValueMember = "MaKho";

            cbbDonViSanXuat.DataSource = cn.getTable("select * from tbl_DoiTac where DonViSanXuat=1");
            cbbDonViSanXuat.DisplayMember = "TenDoiTac";
            cbbDonViSanXuat.ValueMember = "MaDoiTac";

            cbbCongTrinhDuAn.DataSource = cn.getTable("select * from tbl_CongTrinhDuAn");
            cbbCongTrinhDuAn.DisplayMember = "TenCongTrinhDuAn";
            cbbCongTrinhDuAn.ValueMember = "MaCongTrinhDuAn";

            load_dtgPhieuXuat();
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "where 1=1 ";

            if (ckbSoPhieu.Checked && txtSoPhieu.Text.Trim() != "")
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.SoPhieu like '%" + txtSoPhieu.Text.Trim() + "%'";
            }

            if (ckbKhoXuat.Checked == true && cbbKhoXuat.Items.Count > 0)
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.MaKho='" + cbbKhoXuat.SelectedValue.ToString() + "'";
            }

            if (ckbDonViSanXuat.Checked == true && cbbDonViSanXuat.Items.Count > 0)
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.MaDoiTac='" + cbbDonViSanXuat.SelectedValue.ToString() + "'";
            }

            if (ckbNguoiLapPhieu.Checked == true && txtNguoiLapPhieu.Text.Trim() != "")
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.NguoiXuat like '%" + txtNguoiLapPhieu.Text.Trim() + "%'";
            }

            if (ckbCongTrinhDuAn.Checked == true && cbbCongTrinhDuAn.Items.Count > 0)
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.MaCongTrinhDuAn='" + cbbCongTrinhDuAn.SelectedValue.ToString() + "'";
            }

            if (ckbNgayXuat.Checked == true)
            {
                dieuKienLoc += " and tbl_PhieuXuatKho.NgayXuat between '" + dtipTuNgay.Value.ToString("yyyy-MM-dd") + "' and '" + dtipDenNgay.Value.ToString("yyyy-MM-dd") + "'";
            }

            return dieuKienLoc;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            load_dtgPhieuXuat(XacDinhDieuKienLoc());
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuat.SelectedRows.Count > 0 && dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                string SoPhieu = dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                frmInPhieuXuatKhoCongTrinhDuAn frm = new frmInPhieuXuatKhoCongTrinhDuAn();
                List<PhieuXuatKhoDVSX> list = new List<PhieuXuatKhoDVSX>();
                string Ngay = "";
                string Thang = "";
                string Nam = "";
                string NguoiXuatHang = "";
                string DonVi = "";
                string CongTrinh = "";
                string DienGiai = "";
                string TenKho = "";

                string STT = "";
                string Kho = "";
                string MaVT = "";
                string TenVatTu = "";
                string DVT = "";
                string HangMuc = "";
                string SL = "";
                string Gia = "";
                string Tien = "";

                string sql = "";
                DataTable dt = new DataTable();

                sql = @"
                    SELECT     dbo.tbl_PhieuXuatKho.NgayXuat, dbo.tbl_PhieuXuatKho.NguoiXuat, tbl_PhieuXuatKho.DienGiai, tbl_LoaiPhieu.TenLoaiPhieu, dbo.tbl_DoiTac.TenDoiTac, dbo.tbl_CongTrinhDuAn.TenCongTrinhDuAn, dbo.tbl_Kho.TenKho
                    FROM         dbo.tbl_PhieuXuatKho INNER JOIN
                                          dbo.tbl_DoiTac ON dbo.tbl_PhieuXuatKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                          dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuXuatKho.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn INNER JOIN
                                          dbo.tbl_Kho ON dbo.tbl_PhieuXuatKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
											tbl_LoaiPhieu ON tbl_PhieuXuatKho.MaLoaiPhieu=tbl_LoaiPhieu.MaLoaiPhieu
                    WHERE dbo.tbl_PhieuXuatKho.SoPhieu = '" + SoPhieu + "'";
                dt = cn.getTable(sql);
                if (dt.Rows.Count > 0)
                {
                    string NgayXuat = dt.Rows[0]["NgayXuat"].ToString();
                    DateTime dtNgayXuat = DateTime.Parse(NgayXuat);
                    Ngay = dtNgayXuat.Day.ToString();
                    Thang = dtNgayXuat.Month.ToString();
                    Nam = dtNgayXuat.Year.ToString();
                    NguoiXuatHang = dt.Rows[0]["NguoiXuat"].ToString();
                    DonVi = dt.Rows[0]["TenDoiTac"].ToString();
                    CongTrinh = dt.Rows[0]["TenCongTrinhDuAn"].ToString();
                    TenKho = dt.Rows[0]["TenKho"].ToString();
                    HangMuc = dt.Rows[0]["TenLoaiPhieu"].ToString();
                    DienGiai=dt.Rows[0]["DienGiai"].ToString();
                }

                sql = @"
                    SELECT     dbo.tbl_PhieuXuatKho.SoPhieu, dbo.tbl_PhieuXuatKho.MaKho, dbo.tbl_ChiTietPhieuXuat.MaVatTu, dbo.tbl_VatTu.TenVatTu, dbo.tbl_DonViTinh.TenDonViTinh,dbo.tbl_ChiTietPhieuXuat.SoLuong
                    FROM         dbo.tbl_ChiTietPhieuXuat INNER JOIN
                                          dbo.tbl_PhieuXuatKho ON dbo.tbl_ChiTietPhieuXuat.SoPhieu = dbo.tbl_PhieuXuatKho.SoPhieu INNER JOIN
                                          dbo.tbl_Kho ON dbo.tbl_PhieuXuatKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                          dbo.tbl_VatTu ON dbo.tbl_ChiTietPhieuXuat.MaVatTu = dbo.tbl_VatTu.MaVatTu INNER JOIN
                                          dbo.tbl_DonViTinh ON dbo.tbl_ChiTietPhieuXuat.MaDonViTinh = dbo.tbl_DonViTinh.MaDonViTinh   
                    WHERE dbo.tbl_PhieuXuatKho.SoPhieu = '" + SoPhieu + "'";
                dt = cn.getTable(sql);
                int stt = 0;
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        stt++;
                        PhieuXuatKhoDVSX fxk = new PhieuXuatKhoDVSX();
                        fxk.Ngay = Ngay;
                        fxk.Thang = Thang;
                        fxk.Nam = Nam;
                        fxk.NguoiXuatHang = NguoiXuatHang;
                        fxk.DonVi = DonVi;
                        fxk.CongTrinh = CongTrinh;
                        fxk.DienGiai = DienGiai;
                        fxk.TenKho = TenKho;

                        STT = stt.ToString();
                        Kho = dr["MaKho"].ToString();
                        MaVT = dr["MaVatTu"].ToString();
                        TenVatTu = dr["TenVatTu"].ToString();
                        DVT = dr["TenDonViTinh"].ToString();
                        SL = dr["SoLuong"].ToString();

                        fxk.STT = STT;
                        fxk.Kho = Kho;
                        fxk.MaVT = MaVT;
                        fxk.TenVatTu = TenVatTu;
                        fxk.DVT = DVT;
                        fxk.HangMuc = HangMuc;
                        fxk.SL = SL;
                        fxk.Gia = Gia;
                        fxk.Tien = Tien;

                        list.Add(fxk);
                    }
                }

                frm.PhieuXuatKhoDVSXBindingSource.DataSource = list;
                frm.ShowDialog();
            }
        }

        private void ckbNgayXuat_CheckedChanged(object sender, EventArgs e)
        {
            dtipTuNgay.Enabled = ckbNgayXuat.Checked;
            dtipDenNgay.Enabled = ckbNgayXuat.Checked;
        }

        private void radLoad_CheckedChanged(object sender, EventArgs e)
        {
            if (radLoad.Checked == true)
            {
                radLoad.Checked = false;
                load_dtgPhieuXuat();
                ckbNgayXuat.Checked = false;
                ckbDonViSanXuat.Checked = false;
                ckbKhoXuat.Checked = false;
                ckbCongTrinhDuAn.Checked = false;
                ckbNguoiLapPhieu.Checked = false;
                ckbSoPhieu.Checked = false;
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (dtgPhieuXuat.SelectedRows.Count > 0 && dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value != null)
            {

                foreach (DataGridViewRow dgvr in dtgChiTiet.Rows)
                {
                    if (dgvr.IsNewRow) continue;
                    string sql = "SELECT MaKho FROM dbo.tbl_PhieuXuatKho WHERE SoPhieu = '" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                    string MaKho = cn.LayDuLieu(sql);
                    string MaVatTu = dgvr.Cells["colMaVatTu"].Value.ToString();
                    string SoPhieu = dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                    string NgayLapPhieu = dtgPhieuXuat.CurrentRow.Cells["colNgayXuat"].Value.ToString();

                    float SoLuongDeNghi = 0;
                    sql = "select SoLuong from tbl_ChiTietPhieuDeNghiCap where SoPhieu='" + dtgPhieuXuat.CurrentRow.Cells["MaPhieuDeNghi"].Value.ToString() + "' and MaVatTu='" + MaVatTu + "'";
                    DataTable dtSoLuongDeNghi = cn.getTable(sql);
                    SoLuongDeNghi = float.Parse(dtSoLuongDeNghi.Rows[0]["SoLuong"].ToString());

                    float SoLuongDaCap = 0;
                    sql = @"
                            SELECT	SUM(dbo.tbl_ChiTietPhieuXuat.SoLuong) as SoLuongDaCap
                            FROM         dbo.tbl_PhieuXuatKho INNER JOIN
                                                  dbo.tbl_ChiTietPhieuXuat ON dbo.tbl_PhieuXuatKho.SoPhieu = dbo.tbl_ChiTietPhieuXuat.SoPhieu
                            WHERE dbo.tbl_PhieuXuatKho.MaPhieuDeNghi='" + dtgPhieuXuat.CurrentRow.Cells["MaPhieuDeNghi"].Value.ToString() + "' and tbl_PhieuXuatKho.XacNhan=1 AND dbo.tbl_ChiTietPhieuXuat.MaVatTu ='" + MaVatTu + @"'
                            GROUP BY dbo.tbl_ChiTietPhieuXuat.MaVatTu
                        ";
                    DataTable dtSoLuong = cn.getTable(sql);
                    if (dtSoLuong.Rows.Count >0)
                    {
                        SoLuongDaCap = float.Parse(dtSoLuong.Rows[0]["SoLuongDaCap"].ToString());
                    }

                    float SoLuongTonKho = 0;
                    sql = @"
                                SELECT     SUM(SoLuong) as SoLuongTonKho
                                FROM         dbo.tbl_LuuKho
                                WHERE MaKho = '" + MaKho + "' AND MaVatTu = '" + MaVatTu + @"'
                                GROUP BY MaKho, MaVatTu";
                   DataTable dt = cn.getTable(sql);
                    if (dt.Rows.Count > 0) SoLuongTonKho = float.Parse(dt.Rows[0]["SoLuongTonKho"].ToString());

                    string strSoLuong = "";
                    double SoLuong = 0;

                    if (dgvr.Cells["colSoLuong"].Value.ToString().Contains(','))
                    {
                        strSoLuong = dgvr.Cells["colSoLuong"].Value.ToString().Replace(',', '.');
                    }
                    else
                    {
                        strSoLuong = dgvr.Cells["colSoLuong"].Value.ToString();
                    }
                    double.TryParse(strSoLuong, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out SoLuong);

                    if (SoLuong > Math.Round((SoLuongDeNghi - SoLuongDaCap),2) || SoLuong > SoLuongTonKho)
                    {
                        MessageBox.Show("Không thể xác nhận phiếu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        DialogResult msg = MessageBox.Show("Sau khi xác nhận bạn không thể sửa và xóa phiếu!", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (msg == DialogResult.Yes)
                        {
                            cn.Update_Table("tbl_PhieuXuatKho", "XacNhan=1", "SoPhieu='" + dtgPhieuXuat.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");

                            double SoLuongXuat = -SoLuong;
                            cn.Insert_Table("tbl_LuuKho", "SoPhieu,MaKho,MaVatTu,SoLuong,SoLuongPhePham,NgayCapNhat", "'" + SoPhieu + "','" + MaKho + "','" + MaVatTu + "','" + SoLuongXuat.ToString().Replace(",",".") + "',0,'" + NgayLapPhieu + "'");

                        }
                        load_dtgPhieuXuat();
                    }
                }
            }
        }

    }
}
